//
//  CouponCommentFV.m
//  penzer
//
//  Created by 还带大道 on 2018/11/12.
//  Copyright © 2018年 caiyc. All rights reserved.
//

#import "CouponCommentFV.h"
#import "IQTextView.h"
#import "TZImagePickerController.h"


@interface CouponCommentFV()<UITextViewDelegate,TZImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *completeBtn;
@property (weak, nonatomic) IBOutlet IQTextView *textView;
@property (weak, nonatomic) IBOutlet UIView *picView;
@property (weak, nonatomic) IBOutlet UILabel *promptL;
@property (nonatomic,strong) NSArray *selectedAssets;
@property (nonatomic,strong) NSArray<UIImage *> *photos;
@property (nonatomic,strong) NSMutableArray<UIButton *> *items;

@end

@implementation CouponCommentFV

+ (instancetype)shareView
{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].lastObject;
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    _completeBtn.layer.cornerRadius = 5;
    _promptL.text = @"上传满3张图\n可获得400经验值";
    _textView.placeholder = @"宝贝满足你的期待吗？说说他的优点和美中不足吧";
    _textView.delegate = self;
    _textView.backgroundColor = [UIColor whiteColor];
    _textView.font = [UIFont systemFontOfSize:13];
    self.picView.backgroundColor = [UIColor whiteColor];
}



- (IBAction)selectPhotos {
    
    __weak typeof(self) weakSelf = self;
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:6 delegate:self];
    imagePickerVc.alwaysEnableDoneBtn = YES;
    imagePickerVc.allowPickingOriginalPhoto = NO;
    imagePickerVc.allowPickingVideo = NO;
    
    //可以通过block或者代理，来得到用户选择的照片.
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto){
        
        weakSelf.photos = photos;
        weakSelf.selectedAssets = assets;
        [self addItem];
    }];

    // 目前已经选中的图片数组
    imagePickerVc.selectedAssets = [NSMutableArray arrayWithArray:self.selectedAssets];
    [_showPickerVc presentViewController:imagePickerVc animated:YES completion:nil];
    
}


- (void)addItem
{
    [self.items removeAllObjects];
    [self.picView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    
    CGFloat wh = 80;
   __block CGFloat x = 0;
   __block CGFloat y = 0;
   __block NSInteger row = 0;
   __block NSInteger col = 0;
    NSInteger maxcount = 3;
    CGFloat margin_xy = 15;
    [self.photos enumerateObjectsUsingBlock:^(UIImage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [UIButton new];
        [btn setImage:obj forState:UIControlStateNormal];
        [self.picView addSubview:btn];
        [self.items addObject:btn];
        col = idx % maxcount;
        row = idx / maxcount;
        x = (wh + margin_xy)*col;
        y = (wh + margin_xy)*row;
        btn.frame = CGRectMake(x, y, wh, wh);
    }];
    CGFloat h1 = CGRectGetMinY(self.picView.frame);
    CGFloat h2 = CGRectGetMaxY(self.items.lastObject.frame);
    self.height = h1 + h2 + 100;
    !self.refreshBlock?:self.refreshBlock();
    [self layoutIfNeeded];
}



- (IBAction)complete {
    !self.completeBlock?:self.completeBlock();
}


#pragma mark--setter&&getter

- (NSMutableArray<UIButton *> *)items
{
    if(!_items){
        _items = [NSMutableArray array];
    }
    return _items;
}
@end
